package com.xc.service.impl;

import com.xc.dto.ResultDTO;
import com.xc.entity.UserNavEntity;
import com.xc.entity.UserNavManyEntity;
import com.xc.mapper.UserNavMapper;
import com.xc.service.UserNavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserNavServiceImpl implements UserNavService {
    @Autowired
    private UserNavMapper userNavMapper;


    @Override
    public ResultDTO add(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.add(userNavEntity))
                .build();
    }

    /**
     * 更新用户角色，如果
     * @param userNavEntity
     * @return
     */
    @Override
    public ResultDTO update(UserNavEntity userNavEntity) {
        if (userNavEntity.getId() == null) {
            return add(userNavEntity);
        }
        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.update(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO delete(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.delete(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO updateEnable(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.updateEnable(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO findById(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(0)
                .data(userNavMapper.findById(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO findAll(UserNavEntity userNavEntity) {

        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.count(userNavEntity))
                .data(userNavMapper.findAll(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO count(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.count(userNavEntity))
                .build();
    }

    @Override
    public ResultDTO findByName(UserNavEntity userNavEntity) {
        return null;
    }

    @Override
    public ResultDTO addMany(List<UserNavManyEntity> list) {
        List<Integer> ids=new ArrayList<>();
        list.forEach((item)->ids.add(item.getUserId()));
        userNavMapper.deleteByUserId(ids);

        return ResultDTO.builder()
                .code(0)
                .count(userNavMapper.addMany(list))
                .build();
    }

    @Override
    public ResultDTO findByUserId(UserNavEntity userNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(0)
                .data(userNavMapper.findByUserId(userNavEntity))
                .build();
    }
}
